#!/bin/sh
# 
# A script to maintain a local working copy of an oxidized configuration
# repository and mail out diffs for configuration changes
# 
# Copyright 2016 Nick Hilliard <nick@foobar.org>, All Rights Reserved
# 
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# 
#   http://www.apache.org/licenses/LICENSE-2.0
# 
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# 20170615 - Andre Sencioles <asenci@gmail.com>
#            Removed requirement for Git working directory
#            Check job status
#
# 20200926 - Nick Hilliard <nick@foobar.org>
#            sends email
#            add command-line options to stop blank emails from being sent
#            more sanity-checking
#
# usage: add the following hook to the oxidized config file:
# 
# --
# hooks:
#  email_output:
#    type: exec
#    events: [post_store, node_fail]
#    cmd: '/home/oxidized/extra/oxidized-report-git-commits -s "Oxidized updates for ${OX_NODE_NAME}" -r update-recipient@example.com'
#    async: true
#    timeout: 120
# --
#
# Options:
#         -r email_recipient    - send email to specified recipient
#         -s email_subject      - specify the email subject line
#         -f                    - don't send email if git fails to find commit
# 

trap '/bin/rm -f "$tmpfile"' EXIT

tmpfile=$(mktemp) || exit 1

subject="Oxidized updates for ${OX_NODE_NAME}"
scriptname=`basename $0`

usage()
{
    echo "Usage: ${scriptname} [-f] [ -s email_subject ] [ -r email_recipient ]"
    exit 1
}

email_on_gitfail=1
while getopts "fs:r:" opt; do
    case $opt in
        s) 
            subject=$OPTARG
            ;;
        r)
            recipient=$OPTARG
            ;;
        f)
            email_on_gitfail=0
            ;;
        *)
            usage
            ;;
    esac
done


if [ "${OX_EVENT}" = "node_fail" ]; then
    echo "${scriptname}: ${OX_NODE_NAME}": 'Job failed'
    exit 64
fi

if [ -z "${OX_REPO_COMMITREF}" ]; then
	echo "${scriptname}: "'$OX_REPO_COMMITREF not set'
	exit 64
fi

if [ -z "${OX_REPO_NAME}" ]; then
	echo "${scriptname}: "'$OX_REPO_NAME not set'
	exit 64
fi

cat > ${tmpfile} <<EOF
Node name: ${OX_NODE_NAME}
Group name: ${OX_NODE_GROUP}
Job status: ${OX_JOB_STATUS}
Job time: ${OX_JOB_TIME}
Git repo: ${OX_REPO_NAME}
Git commit ID: ${OX_REPO_COMMITREF}

EOF

# test if commit exists
git --bare --git-dir="${OX_REPO_NAME}" rev-parse --quiet --verify "${OX_REPO_COMMITREF}" > /dev/null 2>&1
gitret=$?

if [ ${gitret} -eq 0 ]; then
    git --bare --git-dir="${OX_REPO_NAME}" show --pretty='' --no-color "${OX_REPO_COMMITREF}" >> ${tmpfile} 2>&1
else
    echo "${scriptname}: commit ${OX_REPO_COMMITREF} does not exist" >> ${tmpfile}
fi

if [ ! -z "${recipient}" -a \( ${gitret} -eq 0 -o ${email_on_gitfail} -eq 1 \) ]; then
    cat ${tmpfile} | mail -s "${subject}" "${recipient}"
else
    cat ${tmpfile}
fi
